package com.tpp.threat_perception_platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpp.threat_perception_platform.dao.ProcessMapper;
import com.tpp.threat_perception_platform.param.MyParam;
import com.tpp.threat_perception_platform.pojo.Process;
import com.tpp.threat_perception_platform.response.ResponseResult;
import com.tpp.threat_perception_platform.service.ProcessService;
import com.tpp.threat_perception_platform.service.RiskAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
public class ProcessServiceImpl implements ProcessService {

    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private RiskAssessmentService riskAssessmentService;

    @Transactional
    @Override
    public int saveProcess(Process process) {

        // 插入
        return processMapper.insertSelective(process);
    }

    @Override
    public void deleteByMacAddress(String macAddress) {
        processMapper.deleteByMacAddress(macAddress);
    }

    @Override
    public ResponseResult processList(MyParam param) {

        // 设置分页参数
        PageHelper.startPage(param.getPage(), param.getLimit());

        // 查询所有
        List<Process> dataList = processMapper.findAll(param);

        // 对每个进程进行风险评估
        for (Process process : dataList) {
            String riskLevel = riskAssessmentService.assess(process); // 调用评估方法
            process.setRiskLevel(riskLevel);
            // 可选：写回数据库
            // processMapper.updateRiskLevelByMacAddress(process.getMacAddress(), riskLevel);
        }

        // 构架pageInfo
        PageInfo<Process> pageInfo = new PageInfo<>(dataList);

        return new ResponseResult<>(pageInfo.getTotal(), pageInfo.getList());
    }
}
